package com.rongshu.sendmessage.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.adapter.PayLogAdapter;
import com.rongshu.sendmessage.base.BaseActivity;
import com.rongshu.sendmessage.base.MyApplication;
import com.rongshu.sendmessage.bean.PayLogBean;
import com.rongshu.sendmessage.bean.UserInfoBean;
import com.rongshu.sendmessage.util.ClientPublicParamUtil;
import com.rongshu.sendmessage.util.DialogUtil;
import com.rongshu.sendmessage.util.ToastUtils;
import com.rongshu.sendmessage.util.httpUtil.Constant;
import com.rongshu.sendmessage.util.httpUtil.HYContent;
import com.rongshu.sendmessage.util.httpUtil.IModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.rongshu.sendmessage.util.DialogUtil.hideProgress;

/**
 * 支付记录
 */
public class PayLogActivity extends BaseActivity {

    private ListView listView;
    private PayLogAdapter mAdapter;
    private ArrayList<PayLogBean> mList;
    private View noresult_view;
    private String userId;
    private SmartRefreshLayout list_refresh;
    private String pullType="";
    private int page=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
        setContentView(R.layout.activity_pay_log);
        userId= MyApplication.mSettings.getString(HYContent.UserId,"");
        initView();
        pullType= Constant.PULLTYPE_DOWN;
        initData();
    }

    private void initData() {
        DialogUtil.showProgress(mContext);
        Map<String, String> param = new HashMap<>();
        param.put("userId",userId);
        param.put("page", page+"");
        param.put("clientPublicParam", ClientPublicParamUtil.getPublicParams(mContext));
        String url= new HYContent().PAY_HISTORY_URL;
        OkGo.<String>post(url).params(param).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if (pullType.equals(Constant.PULLTYPE_DOWN)) {
                    list_refresh.finishRefresh(true);
                    mList.clear();
                } else {
                    list_refresh.finishLoadmore(true);
                }
                IModel.AjaxResult jr = new IModel.AjaxResult(response.body(),false);
                final int code = jr.getCode();
                if(code > 0) {
                    hideProgress();
                    List<PayLogBean> bean = JSON.parseArray(JSON.toJSONString(jr.getJSONArrayData()), PayLogBean.class);
                    mList.addAll(bean);
                    if (mList.size()!=0&&bean.size()==0){
                        ToastUtils.show(mContext,"没有更多了");
                    }
                    mAdapter.notifyDataSetChanged();
                    if (mList.size()==0){
                        noresult_view.setVisibility(View.VISIBLE);
                        listView.setVisibility(View.GONE);
                    }else{
                        noresult_view.setVisibility(View.GONE);
                        listView.setVisibility(View.VISIBLE);

                    }
                }else{
                    ToastUtils.show(mContext,jr.getMsg());
                    hideProgress();
                }
            }
            @Override
            public void onError(Response<String> response) {
                ToastUtils.show(mContext,"操作失败");
                hideProgress();
            }
        });
    }

    private void initView() {
        TextView title=findViewById(R.id.title);
        title.setText("支付记录");
        LinearLayout titleLayLeft = findViewById(R.id.titleLayLeft);
        titleLayLeft.setOnClickListener(view->finish());
        mList=new ArrayList<>();
        mAdapter=new PayLogAdapter(mContext,mList);
        listView=findViewById(R.id.listView);
        noresult_view=findViewById(R.id.noresult_view);
        TextView tv_no_text = findViewById(R.id.tv_no_text);
        tv_no_text.setText("这里什么也没有");
        listView.setAdapter(mAdapter);
        noresult_view.setVisibility(View.GONE);
        listView.setVisibility(View.VISIBLE);
        list_refresh = findViewById(R.id.list_refresh);
        list_refresh.setEnableLoadmore(true);
        list_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pullType= Constant.PULLTYPE_DOWN;
                page=1;
                initData();

            }
        });
        list_refresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pullType = Constant.PULLTYPE_UP;
                page++;
                initData();
            }
        });
    }
}